package com.test.daily.leetcode.y2022.m07.day0718.v01;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/18 下午 02:48
 * @version: 1.0
 */
public class Solution {
    public static class Node{
        int value;
        Node left;
        Node right;
        public Node(int value){
            this.value = value;
        }
    }
    public int nodeNumMax(Node root){
        int max = 0;
        int preLevel = 1;
        int levelNodes = 0;
        HashMap<Node, Integer> nodeLevel = new HashMap<>();
        nodeLevel.put(root, 1);
        Queue<Node> queue = new LinkedList<>();

        queue.add(root);
        while(!queue.isEmpty()){
            Node node = queue.poll();
            int curLevel = nodeLevel.get(node);
            if(node.left != null){
                nodeLevel.put(node.left, curLevel + 1);
                queue.add(node.left);
            }
            if(node.right != null){
                nodeLevel.put(node.right, curLevel + 1);
                queue.add(node.right);
            }
            if(preLevel == curLevel){
                levelNodes++;
            } else {
                max = Math.max(max, levelNodes);
                levelNodes = 1;
                preLevel++;
            }
        }
        max = Math.max(max, levelNodes);
        return max;
    }
    public int maxNodeNums(Node head){
        int max = 0;
        Node curEnd = head;
        Node nextEnd = null;
        int levelNodeNum = 0;
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        while(!queue.isEmpty()){
            Node cur = queue.poll();
            if(cur.left != null){
                queue.add(cur.left);
                nextEnd = cur.left;
            }
            if(cur.right != null){
                queue.add(cur.right);
                nextEnd = cur.right;
            }
            levelNodeNum++;
            if(cur == curEnd){
                max = Math.max(max, levelNodeNum);
                curEnd = nextEnd;
                levelNodeNum = 0;
            }
        }
        return max;
    }
}
